package com.beidouapp.esb.pojo;

import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "QueryResponse")
public class QueryResponse<T> extends CommonResponse {

	private boolean flag = false;
	private Integer offset;
	private Integer limit;
	private int totalcount;
	private List<?> results;
	private T result;

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public List<?> getResults() {
		return results;
	}

	public void setResults(List<?> results) {
		this.results = results;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public static <T> QueryResponse<T> createSuccessfulSingleExecuteResult(
			T entry) {
		QueryResponse<T> info = new QueryResponse<T>();
		info.setFlag(true);
		info.setResult(entry);
		info.setErrorcode(0l);
		info.setTotalcount(1);
		info.setMessage("success");
		info.setDate((new Date().getTime()));
		return info;
	}

	public static <T> QueryResponse<T> createSuccessfulExecuteResults(
			List<T> entry) {
		QueryResponse<T> info = new QueryResponse<T>();
		info.setFlag(true);
		info.setResults(entry);
		info.setErrorcode(0l);
		info.setTotalcount(entry == null ? 0 : entry.size());
		info.setMessage("success");
		info.setDate((new Date().getTime()));
		return info;
	}

	public static <T> QueryResponse<T> createSuccessfulExecuteResults(
			List<T> entry, Integer offset, Integer limit, int totalcount) {
		QueryResponse<T> info = new QueryResponse<T>();
		info.setFlag(true);
		info.setResults(entry);
		info.setLimit(limit);
		info.setOffset(offset);
		info.setErrorcode(0l);
		info.setTotalcount(totalcount);
		info.setMessage("success");
		info.setDate((new Date().getTime()));
		return info;
	}

	public static <T> QueryResponse<T> createFailedExecuteResult(String message) {
		QueryResponse<T> info = new QueryResponse<T>();
		info.setFlag(false);
		info.setErrorcode(1l);
		info.setMessage(message);
		return info;
	}

	public static <T> QueryResponse<T> createFailedExecuteResult(
			Integer statusCode, String message) {
		QueryResponse<T> info = new QueryResponse<T>();
		info.setFlag(false);
		info.setErrorcode(statusCode);
		info.setMessage(message);
		return info;
	}

}
